#include <stdio.h>
#include <stack>
#include "StackUnit.h"


template <typename T>
bool uncouple(std::stack<T>& s, const T& target)
{
    std::stack<T> tmpStk;
    bool foundTarget = false;
    while (!s.empty() && s.top() != target)
    {
        tmpStk.push(s.top());
        s.pop();
    }
    
    if (!s.empty())
    {
        s.pop();
        foundTarget = true;
    }
    
    while (!tmpStk.empty())
    {
        s.push(tmpStk.top());
        tmpStk.pop();
    }
    
    return foundTarget;
}

template <typename T>
void initStack(T* arry, int len, std::stack<T>& s)
{
    for (int i = len-1; i >= 0; i--)
    {
        s.push(arry[i]);
        printf("%d input stack.\n", arry[i]);
    }
}

int demo_main()
{
    int arr[] = {19,14,37,43,11,12}, i;
    int arrsize = sizeof(arr)/sizeof(int);

    std::stack<int> nStack;
    initStack(arr, arrsize, nStack);
    int nTarget = 14;
    if (uncouple(nStack, nTarget))
    {
        printf("uncoupled %d\n", nTarget);
    }
    else
    {
        printf("%d is not on the stack.\n", nTarget);
    }
    
    while (!nStack.empty())
    {
        printf("%d ", nStack.top());
        nStack.pop();
    }
    printf("\n");
    return 0;
}







int main()
{
    infix2Postfix exp("2*(3+5)%8");
    std::string postfixExp;

    try
    {
        postfixExp = exp.postfix();
        printf("The postfix form is %s\n", postfixExp.c_str());
    }
    catch(const std::exception& e)
    {
        printf("%s\n", e.what());
    }
    
    return 0;
}